package com.gxuwz.attend.dao;
import com.gxuwz.attend.entity.Department;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
//二级学院Dao
public class DepartmentDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 添加二级学院数据
     * @param dep
     */
    public void insertDep(Department dep){
        String sql="insert into sys_department(depID,depName)values(?,?)";
        jdbcTemplate.update(sql,dep.getDepID(),dep.getDepName());
    }

    /**
     * 删除二级学院数据
     * @param depID
     */
    public void deleteDep(String depID){
        String sql="delete from sys_department where depID=?";
        jdbcTemplate.update(sql,depID);
    }

    /**
     * 更新二级学院数据
     * @param dep
     */
    public void updateDep(Department dep){
        String sql="update sys_department set depName=? where depID=?";
        jdbcTemplate.update(sql,dep.getDepName(),dep.getDepID());
    }

    /**
     * 查询全部二级学院数据
     * @return
     */
    public List<Department> queryDeps (){
        String sql = "select * from sys_department";
        List<Object> paramValues = new ArrayList<>();
        List<Department> depList = new ArrayList<>();
        depList = jdbcTemplate.query(sql, paramValues.toArray(), new RowMapper<Department>() {
            @Override
            public Department mapRow(ResultSet resultSet, int i) throws SQLException {
                Department dep=new Department();
                String depID=resultSet.getString("depID");
                String depName=resultSet.getString("depName");
                dep.setDepID(depID);
                dep.setDepName(depName);
                return dep;
            }
        });
        return depList;
    }
    /**
     * 模糊查询二级学院数据
     * @param dep
     * @return
     */
    public List<Department> queryDep (Department dep){
        String sql = "select * from sys_department where 1=1";
        List<Object> paramValues = new ArrayList<>();
        List<Department> depList = new ArrayList<>();
        if(dep != null){
            if(dep.getDepID() != null && !"".equals(dep.getDepID().trim())){
                sql += " and depID=?";
                paramValues.add(dep.getDepID());
            }
            if (dep.getDepName()!=null && !"".equals(dep.getDepName().trim())){
                sql += " and depName like ?";
                paramValues.add(dep.getDepName()+"%");
            }
        }
        depList = jdbcTemplate.query(sql, paramValues.toArray(), new RowMapper<Department>() {
            @Override
            public Department mapRow(ResultSet resultSet, int i) throws SQLException {
                Department dep=new Department();
                String depID=resultSet.getString("depID");
                String depName=resultSet.getString("depName");
                dep.setDepID(depID);
                dep.setDepName(depName);
                return dep;
            }
        });
        return depList;

    }


}
